package com.feiyi.app.fragment;

import android.os.Bundle;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.feiyi.app.MainActivity;
import com.feiyi.app.R;

import com.feiyi.app.activity.ArticleDetailActivity;
import com.feiyi.app.activity.ArticleListActivity;
import com.feiyi.app.activity.GoodsDetailActivity;
import com.feiyi.app.activity.GoodsDetailActivityNew;
import com.feiyi.app.activity.GoodsSearchActivity;
import com.feiyi.app.adapter.HomePageRecommendCategoryAdapter;
import com.feiyi.app.adapter.HomeRecommendFourAdapter;
import com.feiyi.app.adapter.HomeRecommendGoodsAdapter;
import com.feiyi.app.app.AppConstant;
import com.feiyi.app.base.BaseFragment;
import com.feiyi.app.bean.HomePageBean;
import com.feiyi.app.net.HttpMethods;
import com.feiyi.app.view.AutoScrollViewPager;
import com.feiyi.app.view.HeadlinesView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.superluo.textbannerlibrary.ITextBannerItemClickListener;
import com.superluo.textbannerlibrary.TextBannerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 作者 : wlt
 * 创建时间:  2018-07-14
 * QQ : 1346947404
 * 邮箱:wanglt_android@163.com
 * 类说明:首页内容
 * In me the tiger sniffs the rose
 */

public class HomePageFragment extends BaseFragment {

    Unbinder unbinder;

    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.rv_recommend)
    RecyclerView rv_recommend;
    @BindView(R.id.rv_recommend_goods)
    RecyclerView rv_recommend_goods;
    @BindView(R.id.rv_recommend_list)
    RecyclerView rv_recommend_list;

    @BindView(R.id.tv_banner)
    TextBannerView tv_banner;
    @BindView(R.id.rl_search)
    RelativeLayout rl_search;
    @BindView(R.id.scrollview)
    NestedScrollView scrollview;

    /**
     * 轮播图广告实体类集合
     */
    private List<HomePageBean.DataBean.AdsBean> adsList = new ArrayList<>();
    /**
     * 推荐的四个频道数据列表
     */
    private List<HomePageBean.DataBean.CategoryAdBean> faxianList = new ArrayList<>();
    /**
     * 文字轮播内容数据集合
     */
    private List<HomePageBean.DataBean.ArticlesBean> textList = new ArrayList<>();
    /**
     * 首页推荐的频道
     */
    private List<HomePageBean.DataBean.CategorysBean> pindaoList = new ArrayList<>();

    /**
     * 首页推荐商品列表
     */
    private List<HomePageBean.DataBean.GoodsBean> goodsBeanList = new ArrayList<>();

    private static final String TAG = "HomePageFragment";

    /**
     * 频道列表adapter
     */
    private HomePageRecommendCategoryAdapter categoryAdapter;
    /**
     * 顶部轮播图
     */
    private HeadlinesView headView;
    /**
     * 四个推荐广告适配器
     */
    private HomeRecommendFourAdapter fourAdapter;
    /**
     * 商品列表适配器
     */
    private HomeRecommendGoodsAdapter goodsAdapter;
    private int search_height;

    @Override
    public View getLayoutInflater(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.fragment_homepage,null);
        unbinder = ButterKnife.bind(this,view);
        return view;
    }

    @Override
    public void initView() {
        refreshLayout.setEnableLoadMore(false);

        rv_recommend.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (position != 9){
                    Bundle bundle = new Bundle();
                    bundle.putString("category_id",pindaoList.get(position).getCategory_id());
                    ActivityUtils.startActivity(bundle,GoodsSearchActivity.class);
                }else {
                    ((MainActivity)getActivity()).changeClassify();
                }
            }
        });
        rv_recommend_list.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                switch (faxianList.get(position).getModel()){
                    case "article":
                        bundle.putString("article_id",faxianList.get(position).getModel_id());
                        ActivityUtils.startActivity(bundle, ArticleDetailActivity.class);
                        break;
                    case "goods":
                        bundle.putString("goods_id",faxianList.get(position).getModel_id());
                        ActivityUtils.startActivity(bundle, GoodsDetailActivityNew.class);
                        break;
                    case "category":
                        bundle.putString("category_id",faxianList.get(position).getModel_id());
                        ActivityUtils.startActivity(bundle, GoodsSearchActivity.class);
                        break;
                    case "brand":
                        bundle.putString("brand_id",faxianList.get(position).getModel_id());
                        ActivityUtils.startActivity(bundle, GoodsSearchActivity.class);
                        break;

                }
            }
        });
        rv_recommend_goods.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("goods_id",goodsBeanList.get(position).getGoods_id());
                ActivityUtils.startActivity(bundle,GoodsDetailActivityNew.class);
            }
        });
    }

    @Override
    public void initDate() {
        HttpMethods.getInstance().info.getHomePage1(AppConstant.HOME_URL)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<HomePageBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(HomePageBean homePageBean) {
                        if(null != homePageBean.getData().getAds())
                            adsList.addAll(homePageBean.getData().getAds());
                        if(null != homePageBean.getData().getCategory_ad())
                           faxianList.addAll(homePageBean.getData().getCategory_ad());
                        if(null != homePageBean.getData().getArticles())
                            textList.addAll(homePageBean.getData().getArticles());
                        if(null != homePageBean.getData().getCategorys())
                            pindaoList.addAll(homePageBean.getData().getCategorys());
                        if(null != homePageBean.getData().getGoods())
                            goodsBeanList.addAll(homePageBean.getData().getGoods());

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(TAG, "onError: "+e.toString() );
                        ToastUtils.showShort("获取网络数据失败");
                    }

                    @Override
                    public void onComplete() {

                        fillData();
                    }
                });

    }

    @Override
    public void initAdapter() {

        categoryAdapter = new HomePageRecommendCategoryAdapter(R.layout.item_home_category,pindaoList);
        rv_recommend.setLayoutManager(new GridLayoutManager(activity,5));
        rv_recommend.setAdapter(categoryAdapter);

        fourAdapter = new HomeRecommendFourAdapter(R.layout.item_home_recommend_four,faxianList);
        rv_recommend_list.setLayoutManager(new GridLayoutManager(activity,2));
        rv_recommend_list.setAdapter(fourAdapter);

        goodsAdapter = new HomeRecommendGoodsAdapter(R.layout.item_home_goods,goodsBeanList);
        rv_recommend_goods.setLayoutManager(new GridLayoutManager(activity,2));
        rv_recommend_goods.setAdapter(goodsAdapter);

    }

    /**
     * 获取玩网络数据,填充数据的过程
     *
     */
    private void fillData(){
        fillTop();
        fillText();
        fillRecommendCategory();
        fillGoods();

    }

    /**
     * 填充顶部数据,包括顶部轮播图和推荐频道
     */
    private void fillTop(){
        HomePageBean.DataBean.CategorysBean categorysBean = new HomePageBean.DataBean.CategorysBean();
        categorysBean.setName("更多");
        pindaoList.add(categorysBean);
        headView = new HeadlinesView(activity);
        headView.initData(adsList);
        categoryAdapter.setHeaderView(headView);
        headView.startAutoScroll();
        categoryAdapter.notifyDataSetChanged();

    }

    /**
     * 填充文字轮播
     */
    private void fillText(){
        ViewGroup.LayoutParams layoutParams = tv_banner.getLayoutParams();
        layoutParams.width = ScreenUtils.getScreenWidth()/2;
        tv_banner.setLayoutParams(layoutParams);
        List<String> wenziList = new ArrayList<>();
        for (int i = 0; i < textList.size(); i++) {
            wenziList.add(textList.get(i).getTitle());
        }
        tv_banner.setDatas(wenziList);
        tv_banner.setItemOnClickListener(new ITextBannerItemClickListener() {
            @Override
            public void onItemClick(String s, int position) {
//                ToastUtils.showShort(textList.get(position).getArticle_id());
                Bundle bundle = new Bundle();
                bundle.putString("article_id",textList.get(position).getArticle_id());
                ActivityUtils.startActivity(bundle,ArticleDetailActivity.class);
            }
        });
    }

    /**
     * 填充推荐的四个栏目
     */
    private void fillRecommendCategory(){
        fourAdapter.notifyDataSetChanged();
    }

    /**
     * 填充推荐商品
     */
    private void fillGoods(){
        goodsAdapter.notifyDataSetChanged();
    }

    @OnClick({R.id.rl_search,R.id.tv_gonggao_more})
    public void onClick(View view){

        switch (view.getId()){
            case R.id.rl_search:
                ActivityUtils.startActivity(GoodsSearchActivity.class);
                break;
            case R.id.tv_gonggao_more:
                ActivityUtils.startActivity(ArticleListActivity.class);
                break;
        }


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
